{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "completed-george",
   "metadata": {},
   "source": [
    "# Problem 14.12 Work and Temperature Change Upon Adiabatic Compression of Oxygen"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "objective-entrepreneur",
   "metadata": {},
   "source": [
    "A stream of oxygen is compressed by a compressor from a pressure P1  = 1 bar to P2 = 10 bar. The flow rate of the oxygen stream is 250 kg/h and the temperature is 25°C.\n",
    "\n",
    "What is the power of the compressor, and the outlet temperature of the gas?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "historical-liquid",
   "metadata": {},
   "source": [
    "## Solution\n",
    "\n",
    "This is a series of PH, PS and PT flashes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "rental-privilege",
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy.constants import bar, hour\n",
    "from thermo import ChemicalConstantsPackage, SRKMIX, IGMIX, CEOSGas, CEOSLiquid, FlashPureVLS\n",
    "fluid = 'oxygen'\n",
    "constants, correlations = ChemicalConstantsPackage.from_IDs([fluid])\n",
    "\n",
    "T1 = 25 + 273.15\n",
    "P1 = 1*bar\n",
    "P2 = 10*bar\n",
    "zs = [1]\n",
    "eta_isentropic = 0.75\n",
    "eta_mechanical = 0.95"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "straight-asthma",
   "metadata": {},
   "outputs": [],
   "source": [
    "eos_kwargs = dict(Tcs=constants.Tcs, Pcs=constants.Pcs, omegas=constants.omegas)\n",
    "liquid = CEOSLiquid(SRKMIX, HeatCapacityGases=correlations.HeatCapacityGases, eos_kwargs=eos_kwargs)\n",
    "gas = CEOSGas(SRKMIX, HeatCapacityGases=correlations.HeatCapacityGases, eos_kwargs=eos_kwargs)\n",
    "SRK_flasher = FlashPureVLS(constants, correlations, liquids=[liquid], gas=gas, solids=[])\n",
    "\n",
    "gas = CEOSGas(IGMIX, HeatCapacityGases=correlations.HeatCapacityGases, eos_kwargs=eos_kwargs)\n",
    "ideal_flasher = FlashPureVLS(constants, correlations, gas=gas, liquids=[], solids=[])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "completed-worcester",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "With the SRK EOS:\n",
      "The actual power is 24368 W\n",
      "The actual outlet temperature is  643.85 K\n"
     ]
    }
   ],
   "source": [
    "# Flash at inlet conditions to obtain initial enthalpy\n",
    "state_1 = SRK_flasher.flash(T=T1, P=P1)\n",
    "# Flash at outlet condition - entropy is conserved by compressors and expanders!\n",
    "state_2_ideal = SRK_flasher.flash(S=state_1.S(), P=P2)\n",
    "# Compute the change in enthalpy\n",
    "delta_H_ideal = (state_2_ideal.H()-state_1.H())\n",
    "# The definition of isentropic efficiency means that the actual amount of heat added is\n",
    "# dH_actual = dH_idea/eta_isentropic\n",
    "H_added_to_fluid_actual = delta_H_ideal/eta_isentropic\n",
    "\n",
    "state_2 = SRK_flasher.flash(H=state_1.H() + H_added_to_fluid_actual, P=P2)\n",
    "\n",
    "# To compute the actual power, itis more convinient to use the mass enthalpy\n",
    "actual_power_per_kg = (state_2.H_mass() - state_1.H_mass())/(eta_mechanical) # W/kg\n",
    "actual_power = actual_power_per_kg*250/hour\n",
    "print('With the SRK EOS:')\n",
    "print(f'The actual power is {actual_power:.0f} W')\n",
    "print(f'The actual outlet temperature is {state_2.T: .2f} K')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "marked-blink",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "With the ideal EOS:\n",
      "The actual power is 24341 W\n",
      "The actual outlet temperature is  643.68 K\n"
     ]
    }
   ],
   "source": [
    "# Flash at inlet conditions to obtain initial enthalpy\n",
    "state_1 = ideal_flasher.flash(T=T1, P=P1)\n",
    "# Flash at outlet condition - entropy is conserved by compressors and expanders!\n",
    "state_2_ideal = ideal_flasher.flash(S=state_1.S(), P=P2)\n",
    "# Compute the change in enthalpy\n",
    "delta_H_ideal = (state_2_ideal.H()-state_1.H())\n",
    "# The definition of isentropic efficiency means that the actual amount of heat added is\n",
    "# dH_actual = dH_idea/eta_isentropic\n",
    "H_added_to_fluid_actual = delta_H_ideal/eta_isentropic\n",
    "\n",
    "state_2 = ideal_flasher.flash(H=state_1.H() + H_added_to_fluid_actual, P=P2)\n",
    "\n",
    "# To compute the actual power, itis more convinient to use the mass enthalpy\n",
    "actual_power_per_kg = (state_2.H_mass() - state_1.H_mass())/(eta_mechanical) # W/kg\n",
    "actual_power = actual_power_per_kg*250/hour\n",
    "print('With the ideal EOS:')\n",
    "print(f'The actual power is {actual_power:.0f} W')\n",
    "print(f'The actual outlet temperature is {state_2.T: .2f} K')"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
